package com.zr.service.impl;

import com.zr.dao.ArticleCateDao;
import com.zr.pojo.ArticleCate;
import com.zr.service.ArticleCateService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class ArticleCateServiceImpl implements ArticleCateService {

    ArticleCateDao articleCateDao;

    @Override
    public Boolean add(ArticleCate articleCate) {
//        TODO:查询是否存在同名文章类别，已做
        ArticleCate cateDB = articleCateDao.selectByName(articleCate.getName());
        if(cateDB != null){
            return false;
        }
        return articleCateDao.insert(articleCate) == 1;
    }



    @Override
    public List<ArticleCate> selectAllCate() {
        return articleCateDao.selectAllCate();
    }

//    逻辑删除
    @Override
    public Boolean deleteById(int id) {
        return articleCateDao.logdeleteById(id) == 1;
    }

//    修改文章分类
    @Override
    public Boolean updateById(ArticleCate articleCate) {
        //查询数据库中是否存在未删除的同名分类,,缺陷,会导致没法修改
//        String newName = articleCate.getName();
//        ArticleCate cateDB = articleCateDao.selectByName(newName);
//        if(cateDB != null){
//            return false;
//        }

        return articleCateDao.updateById(articleCate) == 1;
    }

    @Override
    public ArticleCate selectById(int id) {
        return articleCateDao.selectById(id);
    }
}
